package exam3;


import java.util.List;

public class CPU {
    private static CmdTable cmdTable = new CmdTable();
    private static Memory memory = new Memory();
    private static Disk disk = new Disk();

    public static void main(String[] args){
        initCmdTable();
        memory.initPageTable();
        memory.initMemory();
        runCommand();
    }

    public static void runCommand() {
        List<Cmd> clist = cmdTable.getList();
        for (Cmd cmd : clist) {
            Page page = memory.getPage(cmd.getPageNum());
            System.out.println("[RUN CMD] "+ cmd);
            while (page == null) {
                System.out.println("[PAGE FAULT]: *" + cmd.getPageNum());
                memory.doFIFOReplace(cmd.getPageNum());
                page = memory.getPage(cmd.getPageNum());
            }
            if (cmd.getOperation().equals("存")){
                // 对于“存”命令，执行后需要把修改标志位改为1
                page.setModifySignal(1);
            }
            System.out.println("[SUCCESS]" + " RealAddr: "+ (page.getMemoryNum() * 128 + cmd.getUnitNum()));
            System.out.println();
//            memory.printMem();
//            memory.printPageTable();
        }
        memory.printFIFOQueue();
    }

    public static void initCmdTable(){
        cmdTable.add(new Cmd("+", 0, 70));
        cmdTable.add(new Cmd("+", 1, 50));
        cmdTable.add(new Cmd("×", 2, 15));
        cmdTable.add(new Cmd("存", 3, 21));
        cmdTable.add(new Cmd("取", 0,56));
        cmdTable.add(new Cmd("-", 6, 40));
        cmdTable.add(new Cmd("移位",4,53));
        cmdTable.add(new Cmd("+" ,5 ,23));
        cmdTable.add(new Cmd("存" ,1 ,37));
        cmdTable.add(new Cmd("取" ,2 ,78));
        cmdTable.add(new Cmd("+" ,4 ,1));
        cmdTable.add(new Cmd("存" ,6 ,84));
    }
}
